package com.shankungfu.main;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.shankungfu.GlobleConstant;
import com.shankungfu.MainApplication;
import com.shankungfu.R;
import com.shankungfu.adapter.CommentReplyAdapter;
import com.shankungfu.bean.CommentListDataBean;
import com.shankungfu.bean.CommentReplyBean;
import com.shankungfu.bean.ReplyBean;
import com.shankungfu.util.MyAsyncHttpResponseHandler;
import com.shankungfu.util.ToastUtil;
import com.shankungfu.util.WebService;

public class CommentReplyActivity extends Activity implements OnClickListener,
		OnItemClickListener {
	public static final int SUCCESS_CODE = 100;
	public static final int DENGLU_SUCCESS_CODE = 101;

	private static final int DATA_SUCCESS = 1;

	private RelativeLayout progressBar;
	private ImageView head_refresh;
	private ImageView head_goback;
	private ImageView iv;
	private TextView detail_title;
	private String reply_id = "0";
	private String reply_user_name = "";
	private List<CommentListDataBean> commentReplyList;
	private CommentReplyAdapter adapter;
	private ListView commentListView;

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case DATA_SUCCESS:
				CommentReplyBean commentReplyBean = (CommentReplyBean) msg.obj;
				commentReplyList = commentReplyBean.getCommentListData();
				adapter.setData(commentReplyList);
				adapter.notifyDataSetChanged();
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.comment_layout);

		init();
		updateForData();
		getData();
	}
	
	private void init() {
		reply_id = getIntent().getBundleExtra("replyBean").getString("reply_id");
		reply_user_name = getIntent().getBundleExtra("replyBean").getString("author");
		commentReplyList = new ArrayList<CommentListDataBean>();

		progressBar = (RelativeLayout) findViewById(R.id.comment_progressBar);
		commentListView = (ListView) findViewById(R.id.comment_listview);
		head_refresh = (ImageView) findViewById(R.id.head_refresh);
		head_goback = (ImageView) findViewById(R.id.head_goback);
		iv = (ImageView) findViewById(R.id.iv);
		detail_title = (TextView) findViewById(R.id.detail_title);

		head_refresh.setImageResource(R.drawable.eidt_image);

		detail_title.setText("对@" + reply_user_name + "评论的回复");
		
		commentListView.setOnItemClickListener(this);
		adapter = new CommentReplyAdapter(CommentReplyActivity.this,
				MainApplication.getOptions());
		commentListView.setAdapter(adapter);

		head_goback.setOnClickListener(this);
	}

	private void getData() {
		WebService httpclient = new WebService();
		RequestParams params = new RequestParams();
		params.add("serviceId", "get_reply_comment");
		params.add("app_info", "shankungfu");
		params.add("reply_id", reply_id);

		AsyncHttpResponseHandler responseHandler = new MyAsyncHttpResponseHandler() {
			@Override
			public void onSuccess(String content) {
				getSuccessData();
				if (!TextUtils.isEmpty(content)
						&& content.contains("\"result\":0")) {
					CommentReplyBean commentReplyBean = JSON.parseObject(
							content, CommentReplyBean.class);

					Message msg = new Message();
					msg.what = DATA_SUCCESS;
					msg.obj = commentReplyBean;
					handler.sendMessage(msg);
				}
			}

			@Override
			public void onFailure(String content) {
			}
		};
		httpclient.request(GlobleConstant.GET_REPLY_COMMENT, params,
				responseHandler);
	}

	private void updateForData() {
		progressBar.setVisibility(View.VISIBLE);
	}

	private void getSuccessData() {
		progressBar.setVisibility(View.GONE);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == SUCCESS_CODE) {
			updateForData();
			getData();
		} else if (resultCode == DENGLU_SUCCESS_CODE) {
			Intent intent = new Intent(this, SecondCommentActivity.class);
			intent.putExtra("replyBean", getIntent().getBundleExtra("replyBean"));
			startActivityForResult(intent, SUCCESS_CODE);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		CommentListDataBean commentListDataBean = commentReplyList.get(position);
		
		if (MainApplication.isUserIsLogin()) {
			Intent intent = new Intent(this, SecondCommentActivity.class);
			Bundle bundle = getIntent().getBundleExtra("replyBean");
			bundle.putString("comment", commentListDataBean.getUser_name());
			intent.putExtra("replyBean", bundle);
			startActivityForResult(intent, SUCCESS_CODE);
		} else {
			ToastUtil.showMessageText(this, "请先登录");
			Intent intent = new Intent(this, LogInActivity.class);
			startActivityForResult(intent, DENGLU_SUCCESS_CODE);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.head_goback:
			finishActivity();
			break;

		case R.id.head_refresh:

			if (MainApplication.isUserIsLogin()) {
				Intent intent = new Intent(this, SecondCommentActivity.class);
				intent.putExtra("replyBean", getIntent().getBundleExtra("replyBean"));
				startActivityForResult(intent, SUCCESS_CODE);
			} else {
				ToastUtil.showMessageText(this, "请先登录");
				Intent intent = new Intent(this, LogInActivity.class);
				startActivityForResult(intent, DENGLU_SUCCESS_CODE);
			}
			break;
		}
	}
	
	private void finishActivity() {
		// setResult(PickItemDetailActivity.SUCCESS_CODE);
		finish();
	}
	
}
